﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravelSite.Entities;
using Dapper;

namespace TravelSite.DAL.Repositories
{
    public class ApiContactRepository : BaseRepository<Contact>
    {
        protected override string TableName
        {
            get { return "[Contact]"; }
        }

        public Contact FindByPin(string pin)
        {
            return Connection.Query<Contact>(
                @"  SELECT c.ContactId, c.AgentId, c.FirstName, c.LastName, c.Address1, c.Address2, 
                            c.City, c.CountryId, c.StateId, c.Zip, c.Email, c.MPhone, c.WPhone, c.HPhone, c.CompanyName, c.JobTitle, 
                            c.PartnerFirstName, c.PartnerLastName, c.DateOfBirth, c.Notes,
                            [Country].CountryName, s.StateCode, u.FirstName + ' ' + u.LastName as AgentName
                    FROM [Contact] (nolock) c left join [State] (nolock) s on c.StateId = s.StateId
                                                left join [User] (nolock) u on c.AgentId = u.Id
                                                left join [Country] (nolock) on c.CountryId = [Country].CountryId
                    WHERE c.Pin = @Pin", new { Pin = pin }).SingleOrDefault();
        }

        public string GetPin(int contactId)
        {
            string result = Connection.Query<string>(@"  SELECT c.Pin FROM [Contact] (nolock) c WHERE c.ContactId = @ContactId", 
                new { ContactId = contactId }).SingleOrDefault();

            if (string.IsNullOrEmpty(result))
                result = "";

            return result;
        }
    }
}
